<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>layoutasserts.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_style_layoutasserts.js.html">layoutasserts.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for making layout assertions. This is a port
<a name="line17"></a> * of http://go/layoutbot.java
<a name="line18"></a> * See {@link http://go/layouttesting}.
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.testing.style.layoutasserts&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.style&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.style&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Asserts that an element has:
<a name="line30"></a> *   1 - a CSS rendering the makes the element visible.
<a name="line31"></a> *   2 - a non-zero width and height.
<a name="line32"></a> * @param {Element|string} a The element or optionally the comment string.
<a name="line33"></a> * @param {Element=} opt_b The element when a comment string is present.
<a name="line34"></a> */
<a name="line35"></a>var assertIsVisible = function(a, opt_b) {
<a name="line36"></a>  _validateArguments(1, arguments);
<a name="line37"></a>  var element = nonCommentArg(1, 1, arguments);
<a name="line38"></a>
<a name="line39"></a>  _assert(commentArg(1, arguments),
<a name="line40"></a>      goog.testing.style.isVisible(element) &amp;&amp;
<a name="line41"></a>      goog.testing.style.hasVisibleDimensions(element),
<a name="line42"></a>      &#39;Specified element should be visible.&#39;);
<a name="line43"></a>};
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * The counter assertion of assertIsVisible().
<a name="line48"></a> * @param {Element|string} a The element or optionally the comment string.
<a name="line49"></a> * @param {Element=} opt_b The element when a comment string is present.
<a name="line50"></a> */
<a name="line51"></a>var assertNotVisible = function(a, opt_b) {
<a name="line52"></a>  _validateArguments(1, arguments);
<a name="line53"></a>  var element = nonCommentArg(1, 1, arguments);
<a name="line54"></a>  if (!element) {
<a name="line55"></a>    return;
<a name="line56"></a>  }
<a name="line57"></a>
<a name="line58"></a>  _assert(commentArg(1, arguments),
<a name="line59"></a>      !goog.testing.style.isVisible(element) ||
<a name="line60"></a>      !goog.testing.style.hasVisibleDimensions(element),
<a name="line61"></a>      &#39;Specified element should not be visible.&#39;);
<a name="line62"></a>};
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Asserts that the two specified elements intersect.
<a name="line67"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line68"></a> * @param {Element} b The second element or the first element if comment string
<a name="line69"></a> *     is present.
<a name="line70"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line71"></a> */
<a name="line72"></a>var assertIntersect = function(a, b, opt_c) {
<a name="line73"></a>  _validateArguments(2, arguments);
<a name="line74"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line75"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line76"></a>
<a name="line77"></a>  _assert(commentArg(1, arguments),
<a name="line78"></a>      goog.testing.style.intersects(element, otherElement),
<a name="line79"></a>      &#39;Elements should intersect.&#39;);
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Asserts that the two specified elements do not intersect.
<a name="line85"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line86"></a> * @param {Element} b The second element or the first element if comment string
<a name="line87"></a> *     is present.
<a name="line88"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line89"></a> */
<a name="line90"></a>var assertNoIntersect = function(a, b, opt_c) {
<a name="line91"></a>  _validateArguments(2, arguments);
<a name="line92"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line93"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line94"></a>
<a name="line95"></a>  _assert(commentArg(1, arguments),
<a name="line96"></a>      !goog.testing.style.intersects(element, otherElement),
<a name="line97"></a>      &#39;Elements should not intersect.&#39;);
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Asserts that the element must have the specified width.
<a name="line103"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line104"></a> * @param {Element} b The second element or the first element if comment string
<a name="line105"></a> *     is present.
<a name="line106"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line107"></a> */
<a name="line108"></a>var assertWidth = function(a, b, opt_c) {
<a name="line109"></a>  _validateArguments(2, arguments);
<a name="line110"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line111"></a>  var width = nonCommentArg(2, 2, arguments);
<a name="line112"></a>  var size = goog.style.getSize(element);
<a name="line113"></a>  var elementWidth = size.width;
<a name="line114"></a>
<a name="line115"></a>  _assert(commentArg(1, arguments),
<a name="line116"></a>      goog.testing.style.layoutasserts.isWithinThreshold_(
<a name="line117"></a>          width, elementWidth, 0 /* tolerance */),
<a name="line118"></a>      &#39;Element should have width &#39; + width + &#39; but was &#39; + elementWidth + &#39;.&#39;);
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Asserts that the element must have the specified width within the specified
<a name="line124"></a> * tolerance.
<a name="line125"></a> * @param {Element|string} a The element or optionally the comment string.
<a name="line126"></a> * @param {number|Element} b The height or the element if comment string is
<a name="line127"></a> *     present.
<a name="line128"></a> * @param {number} c The tolerance or the height if comment string is
<a name="line129"></a> *     present.
<a name="line130"></a> * @param {number=} opt_d The tolerance if comment string is present.
<a name="line131"></a> */
<a name="line132"></a>var assertWidthWithinTolerance = function(a, b, c, opt_d) {
<a name="line133"></a>  _validateArguments(3, arguments);
<a name="line134"></a>  var element = nonCommentArg(1, 3, arguments);
<a name="line135"></a>  var width = nonCommentArg(2, 3, arguments);
<a name="line136"></a>  var tolerance = nonCommentArg(3, 3, arguments);
<a name="line137"></a>  var size = goog.style.getSize(element);
<a name="line138"></a>  var elementWidth = size.width;
<a name="line139"></a>
<a name="line140"></a>  _assert(commentArg(1, arguments),
<a name="line141"></a>      goog.testing.style.layoutasserts.isWithinThreshold_(
<a name="line142"></a>          width, elementWidth, tolerance),
<a name="line143"></a>      &#39;Element width(&#39; + elementWidth + &#39;) should be within given width(&#39; +
<a name="line144"></a>      width + &#39;) with tolerance value of &#39; + tolerance + &#39;.&#39;);
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Asserts that the element must have the specified height.
<a name="line150"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line151"></a> * @param {Element} b The second element or the first element if comment string
<a name="line152"></a> *     is present.
<a name="line153"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line154"></a> */
<a name="line155"></a>var assertHeight = function(a, b, opt_c) {
<a name="line156"></a>  _validateArguments(2, arguments);
<a name="line157"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line158"></a>  var height = nonCommentArg(2, 2, arguments);
<a name="line159"></a>  var size = goog.style.getSize(element);
<a name="line160"></a>  var elementHeight = size.height;
<a name="line161"></a>
<a name="line162"></a>  _assert(commentArg(1, arguments),
<a name="line163"></a>      goog.testing.style.layoutasserts.isWithinThreshold_(
<a name="line164"></a>          height, elementHeight, 0 /* tolerance */),
<a name="line165"></a>      &#39;Element should have height &#39; + height + &#39;.&#39;);
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Asserts that the element must have the specified height within the specified
<a name="line171"></a> * tolerance.
<a name="line172"></a> * @param {Element|string} a The element or optionally the comment string.
<a name="line173"></a> * @param {number|Element} b The height or the element if comment string is
<a name="line174"></a> *     present.
<a name="line175"></a> * @param {number} c The tolerance or the height if comment string is
<a name="line176"></a> *     present.
<a name="line177"></a> * @param {number=} opt_d The tolerance if comment string is present.
<a name="line178"></a> */
<a name="line179"></a>var assertHeightWithinTolerance = function(a, b, c, opt_d) {
<a name="line180"></a>  _validateArguments(3, arguments);
<a name="line181"></a>  var element = nonCommentArg(1, 3, arguments);
<a name="line182"></a>  var height = nonCommentArg(2, 3, arguments);
<a name="line183"></a>  var tolerance = nonCommentArg(3, 3, arguments);
<a name="line184"></a>  var size = goog.style.getSize(element);
<a name="line185"></a>  var elementHeight = size.height;
<a name="line186"></a>
<a name="line187"></a>  _assert(commentArg(1, arguments),
<a name="line188"></a>      goog.testing.style.layoutasserts.isWithinThreshold_(
<a name="line189"></a>          height, elementHeight, tolerance),
<a name="line190"></a>      &#39;Element width(&#39; + elementHeight + &#39;) should be within given width(&#39; +
<a name="line191"></a>      height + &#39;) with tolerance value of &#39; + tolerance + &#39;.&#39;);
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Asserts that the first element is to the left of the second element.
<a name="line197"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line198"></a> * @param {Element} b The second element or the first element if comment string
<a name="line199"></a> *     is present.
<a name="line200"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line201"></a> */
<a name="line202"></a>var assertIsLeftOf = function(a, b, opt_c) {
<a name="line203"></a>  _validateArguments(2, arguments);
<a name="line204"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line205"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line206"></a>  var elementRect = goog.style.getBounds(element);
<a name="line207"></a>  var otherElementRect = goog.style.getBounds(otherElement);
<a name="line208"></a>
<a name="line209"></a>  _assert(commentArg(1, arguments),
<a name="line210"></a>      elementRect.left &lt; otherElementRect.left,
<a name="line211"></a>      &#39;Elements should be left to right.&#39;);
<a name="line212"></a>};
<a name="line213"></a>
<a name="line214"></a>
<a name="line215"></a>/**
<a name="line216"></a> * Asserts that the first element is strictly left of the second element.
<a name="line217"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line218"></a> * @param {Element} b The second element or the first element if comment string
<a name="line219"></a> *     is present.
<a name="line220"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line221"></a> */
<a name="line222"></a>var assertIsStrictlyLeftOf = function(a, b, opt_c) {
<a name="line223"></a>  _validateArguments(2, arguments);
<a name="line224"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line225"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line226"></a>  var elementRect = goog.style.getBounds(element);
<a name="line227"></a>  var otherElementRect = goog.style.getBounds(otherElement);
<a name="line228"></a>
<a name="line229"></a>  _assert(commentArg(1, arguments),
<a name="line230"></a>      elementRect.left + elementRect.width &lt; otherElementRect.left,
<a name="line231"></a>      &#39;Elements should be strictly left to right.&#39;);
<a name="line232"></a>};
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * Asserts that the first element is higher than the second element.
<a name="line237"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line238"></a> * @param {Element} b The second element or the first element if comment string
<a name="line239"></a> *     is present.
<a name="line240"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line241"></a> */
<a name="line242"></a>var assertIsAbove = function(a, b, opt_c) {
<a name="line243"></a>  _validateArguments(2, arguments);
<a name="line244"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line245"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line246"></a>  var elementRect = goog.style.getBounds(element);
<a name="line247"></a>  var otherElementRect = goog.style.getBounds(otherElement);
<a name="line248"></a>
<a name="line249"></a>  _assert(commentArg(1, arguments),
<a name="line250"></a>      elementRect.top &lt; otherElementRect.top,
<a name="line251"></a>      &#39;Elements should be top to bottom.&#39;);
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * Asserts that the first element is strictly higher than the second element.
<a name="line257"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line258"></a> * @param {Element} b The second element or the first element if comment string
<a name="line259"></a> *     is present.
<a name="line260"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line261"></a> */
<a name="line262"></a>var assertIsStrictlyAbove = function(a, b, opt_c) {
<a name="line263"></a>  _validateArguments(2, arguments);
<a name="line264"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line265"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line266"></a>  var elementRect = goog.style.getBounds(element);
<a name="line267"></a>  var otherElementRect = goog.style.getBounds(otherElement);
<a name="line268"></a>
<a name="line269"></a>  _assert(commentArg(1, arguments),
<a name="line270"></a>      elementRect.top + elementRect.height &lt; otherElementRect.top,
<a name="line271"></a>      &#39;Elements should be strictly top to bottom.&#39;);
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Asserts that the first element&#39;s bounds contain the bounds of the second
<a name="line277"></a> * element.
<a name="line278"></a> * @param {Element|string} a The first element or optionally the comment string.
<a name="line279"></a> * @param {Element} b The second element or the first element if comment string
<a name="line280"></a> *     is present.
<a name="line281"></a> * @param {Element=} opt_c The second element if comment string is present.
<a name="line282"></a> */
<a name="line283"></a>var assertContained = function(a, b, opt_c) {
<a name="line284"></a>  _validateArguments(2, arguments);
<a name="line285"></a>  var element = nonCommentArg(1, 2, arguments);
<a name="line286"></a>  var otherElement = nonCommentArg(2, 2, arguments);
<a name="line287"></a>  var elementRect = goog.style.getBounds(element);
<a name="line288"></a>  var otherElementRect = goog.style.getBounds(otherElement);
<a name="line289"></a>
<a name="line290"></a>  _assert(commentArg(1, arguments),
<a name="line291"></a>      elementRect.contains(otherElementRect),
<a name="line292"></a>      &#39;Element should be contained within the other element.&#39;);
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/**
<a name="line297"></a> * Returns true if the difference between val1 and val2 is less than or equal to
<a name="line298"></a> * the threashold.
<a name="line299"></a> * @param {number} val1 The first value.
<a name="line300"></a> * @param {number} val2 The second value.
<a name="line301"></a> * @param {number} threshold The threshold value.
<a name="line302"></a> * @return {boolean} Whether or not the the values are within the threshold.
<a name="line303"></a> * @private
<a name="line304"></a> */
<a name="line305"></a>goog.testing.style.layoutasserts.isWithinThreshold_ = function(
<a name="line306"></a>    val1, val2, threshold) {
<a name="line307"></a>  return Math.abs(val1 - val2) &lt;= threshold;
<a name="line308"></a>};
</pre>


</body>
</html>
